package com.zz.yt.lib.bluetooth.delegate.printer;

import android.content.Context;

import com.landicorp.android.eptapi.device.Printer;
import com.landicorp.android.eptapi.exception.RequestException;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public abstract class PrinterStep {


    private PrintLister lister;
    private Context context;
    private Printer.Progress progress;
    private static final int FAIL = 0xff;
    private final Printer printer = Printer.getInstance();
    protected List<Printer.Step> stepList;
    protected PrinterContent printerContent;

    public PrinterStep(PrinterContent printerContent) {
        this.printerContent = printerContent;
    }

    public void init(Context context) {
        this.context = context;
        stepList = new ArrayList<>();
    }

    public void setLister(PrintLister lister) {
        this.lister = lister;
    }

    public int getPrinterStatus() {
        try {
            return Printer.getInstance().getStatus();
        } catch (RequestException e) {
            e.printStackTrace();
        }
        // 返回自定义错误码，表示抛出异常
        return FAIL;
    }

    public boolean addText() {
        if (stepList == null) {
            return false;
        }
        stepList.add(printer -> {
            // 参见 PrinterProgress 类中 progress.doPrint

            Printer.Format format = new Printer.Format();
            addTextContent(format, printer);
        });
        return true;
    }

    protected abstract void addTextContent(Printer.Format format, Printer printer) throws Exception;

    public boolean addBitmap() {
        if (stepList == null) {
            return false;
        }
        stepList.add(printer -> {
            InputStream inputStream = context.getAssets().open("pay.bmp");
            printer.printImage(Printer.Alignment.LEFT, inputStream);
        });
        return true;
    }

    public boolean addBarcode() {
        if (stepList == null) {
            return false;
        }
        stepList.add(this::addBarcodeContent);
        return true;
    }

    /**
     * @param printer:添加条码
     */
    protected abstract void addBarcodeContent(Printer printer);

    public boolean addQRCode() {
        if (stepList == null) {
            return false;
        }
        stepList.add(this::addQRCodeContent);
        return true;
    }

    /**
     * @param printer:二维码打印内容添加
     */
    protected abstract void addQRCodeContent(Printer printer) throws Exception;

    public boolean feedLine(final int line) {
        if (stepList == null) {
            return false;
        }
        stepList.add(printer -> printer.feedLine(line));
        return true;
    }

    public boolean cutPage() {
        if (stepList == null) {
            return false;
        }
        stepList.add(Printer::cutPaper);
        return true;
    }

    public void startStepPrint() {
        if (stepList == null) {
            return;
        }
        progress = new Printer.Progress() {
            @Override
            public void doPrint(Printer printer) {
                // never call
            }

            @Override
            public void onFinish(int error) {
                stepList.clear();
                if (error == Printer.ERROR_NONE) {
                    if (lister != null) {
                        lister.success();
                    }
                } else {
                    if (lister != null) {
                        lister.filed();
                    }
                }
            }

            @Override
            public void onCrash() {
                stepList.clear();
            }
        };
        for (Printer.Step step : stepList) {
            progress.addStep(step);
        }
        try {
            progress.start();
        } catch (RequestException e) {
            e.printStackTrace();
        }
    }

    public abstract boolean addContent();

}
